package com.qqtech.qquan.homemaking.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.frame.controller.BaseControllerImpl;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.enums.HomemakingPubitemStatusDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingPubitem;
import com.qqtech.qquan.homemaking.model.HomemakingPubitemQuery;
import com.qqtech.qquan.homemaking.model.HomemakingPubitemVo;
import com.qqtech.qquan.homemaking.model.HomemakingSysDictQuery;
import com.qqtech.qquan.homemaking.model.HomemakingSysDictVo;
import com.qqtech.qquan.homemaking.service.HomemakingPubitemService;
import com.qqtech.qquan.homemaking.service.HomemakingSysDictService;

@Controller
@RequestMapping(value = "/homemaking/pubitem")
public class HomemakingPubitemController extends
		BaseControllerImpl<HomemakingPubitem, HomemakingPubitemQuery> {
	@Resource
	private HomemakingPubitemService homemakingPubitemService;
	
	@Resource
	private HomemakingSysDictService homemakingSysDictService;

	@Override
	protected BaseService<HomemakingPubitem> getBaseService() {
		return homemakingPubitemService;
	}

	/**
	 * 获取服务项目
	 * @param typeId 类别id
	 * @return
	 */
	@RequestMapping(value = "/findByTypeId")
	@ResponseBody
	public Result findByTypeId(@RequestBody HomemakingPubitemQuery homemakingPubitemQuery) {
		Result result = new Result();
		List<HomemakingPubitemVo> list = homemakingPubitemService.findByTypeId(
				homemakingPubitemQuery.getTypeId(), HomemakingPubitemStatusDictEnum.VALID.getCode());
		//详情页面的服务标准和服务流程
		HomemakingSysDictQuery dictQuery = new HomemakingSysDictQuery(); 
		String code = "type" + homemakingPubitemQuery.getTypeId().toString();
		dictQuery.setCode(code);
		List<HomemakingSysDictVo> dictList = homemakingSysDictService.queryList(dictQuery);
		if (dictList != null && !dictList.isEmpty()) {
			List<Map<String,Object>> tempList = new ArrayList<Map<String,Object>>();
			for (HomemakingSysDictVo dict : dictList) {
				Map<String,Object> tempMap = new HashMap<String, Object>();
				tempList.add(tempMap);
				if (dict.getImgList() == null) { //没有图片的
					tempMap.put("dictTitle", dict.getTypeDesc());
					tempMap.put("dictContent", dict.getName());
					tempMap.put("hasImg", "0");
				} else {
					List<Map<String,Object>> tempContentList = new ArrayList<Map<String,Object>>();
					for (int i = 0; i < dict.getImgList().size(); i++) {
						Map<String,Object> tempContentMap = new HashMap<String, Object>();
						tempContentList.add(tempContentMap);
						tempContentMap.put("dictContent", dict.getNameList().get(i));
						tempContentMap.put("dictContentImg",dict.getFileDomain() + dict.getImgList().get(i));
					}
					tempMap.put("dictTitle", dict.getTypeDesc());
					tempMap.put("dictContents", tempContentList);
					tempMap.put("hasImg", "1");
				}
			}
			result.put("dictList", tempList);
		}
		result.put("pubitemList", list);
		return result;
	}
}
